﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PMS
{
    public partial class BD_UserInfoBLL
    {
        public Message LoginMethod(string usercode, string password)
        {
            View_BD_UserDepInfo user = BLLSession.Current.View_BD_UserDepInfoBLL.GetListBy(u => (u.UserCode == usercode || u.LoginName == usercode) && u.PassWord == password && u.UserState == "0").FirstOrDefault();
            if (user == null)
            {
                return new Message() { state = "passwordError", msg = "用户名密码错误！" };
            }
            else
            {
                List<string> allcodes = new List<string>();
                List<BD_SchoolInfo> alllist = BLLSession.Current.BD_SchoolInfoBLL.GetListBy();
                getAllChildCode(allcodes, user.SchoolCode, alllist);
                string codes = "";
                for (int i = 0; i < allcodes.Count; i++)
                {
                    codes = codes +","+ allcodes[i];
                }
                if (codes.Length>0)
                {
                    codes = codes.Substring(1, codes.Length-1);
                }
                UserInfo usr = new UserInfo() { DepNameSimplified = user.DepName, AccountLogIn = user.AccountLogIn, DepId = user.DepId, DepName = user.DepName, eMail = user.E_Mail, Id = user.ID, Name = user.UserName, PosId = user.PosId, PosName = user.PosName, UserCode = user.UserCode, SchoolCode = user.SchoolCode, SchoolName = user.SchoolName, IfHeadquarters = user.IfHeadquarters, SchoolCodeQuery = user.SchoolCode, SchoolNameQuery = user.SchoolName, SchoolCodeAll = codes };
                HttpContext.Current.Session[OperateContext.InfoKey] = usr;
                HttpContext.Current.Session[OperateContext.PermissionKey] = Permission.GetPermByUserId(usr.Id);
                HttpContext.Current.Session[OperateContext.BtnPermissionKey] = Permission.GetBtnPermByUserId(usr.Id);
                #region 写入Cookie
                //2.1.2将用户id加密成字符串
                string strCookieValue = SecurityHelper.EncryptUserInfo(usr.Id.ToString());
                //2.1.3创建cookie
                HttpCookie cookie = new HttpCookie(OperateContext.InfoKey, strCookieValue);
                cookie.Expires = DateTime.Now.AddDays(1);
                HttpContext.Current.Response.Cookies.Add(cookie);
                #endregion
                //检查和更新意向客户和学员年级
               // CheckAndUpdateStuGrade();
                return new Message() { state = "success", msg = "登陆成功！" };

            }
        }
        private void getAllChildCode(List<string> list, string code, List<BD_SchoolInfo> allschools)
        {
            BD_SchoolInfo info = allschools.Where(u => u.SchoolCode == code).FirstOrDefault();
            list.Add(info.SchoolCode);
            List<string> sch = allschools.Where(u => u.ParentId == info.ID).Select(u => u.SchoolCode).ToList();
            for (int i = 0; i < sch.Count; i++)
            {
                getAllChildCode(list, sch[i], allschools);
            }
        }
     

        public Message EditUserDetail(BD_UserInfo user, List<BD_UserEducation> xueliList, List<BD_UserWorkHistory> workList, List<BD_UserFamily> familyList)
        {
            //user.Age = Commons.String2Int32(DateTime.Now.Year.ToString()) - Commons.String2Int32(user.BirthDate.Substring(0, 4)) + 1;
            DALSession.Current.BD_UserInfoDAL.ModifyBy(user, p => p.ID == user.ID, "DepName", "DepId", "PosName", "PosId", "SchoolName", "SchoolCode", "Sex", "IfMarry", "BirthDate", "UserName", "MinZu", "Huji", "IdNumber", "Address", "Phone", "E_Mail", "Univeristy", "Major", "Note", "ApplySalary", "UserName", "Age", "IfTeacher");
            DALSession.Current.BD_UserEducationDAL.DelBy(d => d.StaffInfoId == user.ID);
            xueliList.ForEach(x =>
            {
                BD_UserEducation xueliInfo = new BD_UserEducation();
                xueliInfo.StaffInfoId = user.ID;
                xueliInfo.StaffCode = user.UserCode;
                xueliInfo.StaffName = user.UserName;
                xueliInfo.EduEndtDate = x.EduEndtDate;
                xueliInfo.EduMajor = x.EduMajor;
                xueliInfo.EduStartDate = x.EduStartDate;
                xueliInfo.EduUniversity = x.EduUniversity;
                xueliInfo.EduXueli = x.EduXueli;
                xueliInfo.Note = x.Note;
                DALSession.Current.BD_UserEducationDAL.Add(xueliInfo);
            });
            DALSession.Current.BD_UserFamilyDAL.DelBy(d => d.UserId == user.ID);
            familyList.ForEach(f =>
            {
                BD_UserFamily familyInfo = new BD_UserFamily();
                familyInfo.UserId = user.ID;
                familyInfo.UserCode = user.UserCode;
                familyInfo.UserName = user.UserName;
                familyInfo.FamilyName = f.FamilyName;
                familyInfo.FamilyPhone = f.FamilyPhone;
                familyInfo.FamilyRelation = f.FamilyRelation;
                familyInfo.FamilyWorkplace = f.FamilyWorkplace;
                familyInfo.note = f.note;
                DALSession.Current.BD_UserFamilyDAL.Add(familyInfo);
            });
            DALSession.Current.BD_UserWorkHistoryDAL.DelBy(d => d.StaffInfoId == user.ID);
            workList.ForEach(w =>
            {
                BD_UserWorkHistory workInfo = new BD_UserWorkHistory();
                workInfo.StaffInfoId = user.ID;
                workInfo.StaffCode = user.UserCode;
                workInfo.StaffName = user.UserName;
                workInfo.LeaveReason = w.LeaveReason;
                workInfo.Note = w.Note;
                workInfo.WorkEndtDate = w.WorkEndtDate;
                workInfo.WorkPlace = w.WorkPlace;
                workInfo.WorkPosition = w.WorkPosition;
                workInfo.WorkStartDate = w.WorkStartDate;
                DALSession.Current.BD_UserWorkHistoryDAL.Add(workInfo);
            });
            DALSession.Current.Savechanges();
            return new Message() { state = "success", msg = "保存成功" };
        }
    }
}